/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package vng.survey.controller;

import hapax.TemplateDataDictionary;
import hapax.TemplateDictionary;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.Arrays;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import vng.survey.memcached.MyCache;
import vng.survey.model.CommonModel;

/**
 *
 * @author namhh
 */
public   abstract  class BaseController extends HttpServlet  {

   
    
    protected void processRequest(HttpServletRequest request, HttpServletResponse response) {
        String out = "";
        try {
            out = render(request, response);
        } catch (Exception ex) {
            out= showError("Đã có lỗi xảy ra. Vui lòng thử lại sau!");
        }
        out(out, response);
    }

   protected String showError(String content) {
        try{
        TemplateDataDictionary dic = TemplateDictionary.create();
        dic.setVariable("HEADER", CommonModel.getHeader(TemplateDictionary.create(), "Trang khảo sát"));
        dic.setVariable("FOOTER", CommonModel.getFooter(TemplateDictionary.create()));
        return CommonModel.getError(dic, content);
        }catch(Exception ex){
            return "<h1>404 Not Found</h1>";
        }
    }
    
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        handle(request, response);
    }
    
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        handle(request, response);
    }
    
    private void handle(HttpServletRequest req, HttpServletResponse resp) {
        try {
            
            prepareHeader(resp);
            processRequest(req, resp);
        } catch (Exception ex) {
            
        }
    }
    
   public abstract String render(HttpServletRequest request, HttpServletResponse resp) throws IOException, Exception;
   public void setCache(String[] value,int timeToLive, Object o) {
        try{
             MyCache.getInstance().set(getKey(value),timeToLive,o);
        }
        catch(Exception ex){
            
        }
    }

    public Object getCache(String[] value) {
        Object o =null;
        try{
           o = MyCache.getInstance().get(getKey(value));
        }
        catch(Exception ex){
            
        }
        return o;
    }
    private void prepareHeader(HttpServletResponse resp) {
        resp.setCharacterEncoding("utf-8");
        resp.setContentType("text/html; charset=UTF-8");
        resp.addHeader("Server", "Zing Me Survey");
    }
    
    private void out(String content, HttpServletResponse respon) {
        PrintWriter out = null;
        try {
            out = respon.getWriter();
            out.print(content);
        } catch (Exception ex) {
            
        } finally {
            if (out != null) {
                out.close();
            }
        }
    }
    protected String getName(){
        return this.getClass().getSimpleName();
    }
    protected String getKey(String [] value){
        String key = getName();
        Arrays.sort(value);
        key+=Arrays.toString(value);
        return key;
    }
}
